/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package inline.calendario;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.DatePicker;
import android.widget.TextView;

/**
 * Basic example of using date and time widgets, including
 * {@link android.app.TimePickerDialog} and {@link android.widget.DatePicker}.
 */
public class Calendario extends Activity {

	// where we display the selected date and time
	private TextView mDateDisplay;
	private DatePicker mDatePicker;
	
	private static final int LISTAR_COMPROMISSOS = Menu.FIRST;

	// date and time
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;

	CalendarioService servico;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		/* Relaciona a interface grafica em XML a atividade */
		setContentView(R.layout.calendario);

		/* Relaciona os Views da tela com variaveis */
		mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
		mDatePicker = (DatePicker) findViewById(R.id.quadro);

		/* Cria um objeto do tipo Calendar, para guardar datas */
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH) + 1;
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);

		servico = new CalendarioService(this);

		mDatePicker.init(mYear, mMonth - 1, mDay, c.getFirstDayOfWeek(), mDateSetListener);

		
		updateDisplay();

	}

	private void updateDisplay() {
		mDateDisplay.setText(new StringBuilder().append(mDay).append("/")
				.append(mMonth).append("/").append(mYear).append(" ").append(
						pad(mHour)).append(":").append(pad(mMinute)));
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			String data, Bundle extras) {		
		super.onActivityResult(requestCode, resultCode, data, extras);
	}
	
	private DatePicker.OnDateSetListener mDateSetListener = new DatePicker.OnDateSetListener() {

		public void dateSet(DatePicker view, int year, int monthOfYear,	int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
			servico.listarCompromissos(mYear, mMonth, mDay);
			updateDisplay();
		}
	};

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}
	
	/* ============================
	 * MENU
	 * =============================
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, LISTAR_COMPROMISSOS, R.string.menu_listar);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, Item item) {
		switch (item.getId()) {
		case LISTAR_COMPROMISSOS:
			mDatePicker.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_NEWLINE));
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

}
